﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BizTalk.Operations;
using OM = Microsoft.BizTalk.Operations;
using System.Collections;

namespace BizTalkConsoleOperations
{
    public class BizTalkWebConsoleOperations
    {
        public static List<ServiceInstances> GetServiceInstances()
        {
            OM.BizTalkOperations bizTalkOperations = new BizTalkOperations();
            IEnumerable omServiceInstancesEnum = bizTalkOperations.GetServiceInstances();

            List<ServiceInstances> serviceInstances = new List<ServiceInstances>();

            foreach (OM.MessageBoxServiceInstance omServiceInstance in omServiceInstancesEnum)
            {
                ServiceInstances serviceInstance = new ServiceInstances();
                serviceInstance.ServiceName = omServiceInstance.ServiceType;
                serviceInstance.Application = omServiceInstance.Application;
                //serviceInstance.ServiceClass = omServiceInstance.Class;
                //serviceInstance.Status = omServiceInstance.InstanceStatus;
                serviceInstance.CreationTime = omServiceInstance.CreationTime.ToString();
                serviceInstance.ErrorCode = omServiceInstance.ErrorCode;
                serviceInstance.ErrorDescription = omServiceInstance.ErrorDescription;
                //serviceInstance.PendingJob = omServiceInstance.PendingOperation;
                serviceInstances.Add(serviceInstance);
            }

            return serviceInstances;
        }

        public static IEnumerable<ServiceInstances> GetServiceInstances(OM.InstanceStatus instanceStatus, int maximumMatches, string ApplicationName, string HostName, OM.ServiceClass ServiceClass)
        {
            OM.BizTalkOperations bizTalkOperations = new BizTalkOperations();

            IEnumerable<OM.MessageBoxServiceInstance> omServiceInstancesEnum = bizTalkOperations.GetServiceInstances().Cast<OM.MessageBoxServiceInstance>();

            switch (instanceStatus)
            {
                case InstanceStatus.All:
                    //omServiceInstancesEnum = from a in omServiceInstancesEnum
                    //                         select a;
                    break;
                case InstanceStatus.RunningAll:
                    omServiceInstancesEnum = from a in omServiceInstancesEnum
                                             where (a.InstanceStatus == InstanceStatus.ReadyToRun
                                             || a.InstanceStatus == InstanceStatus.Active
                                             || a.InstanceStatus == InstanceStatus.Dehydrated
                                             || a.InstanceStatus == InstanceStatus.Reserved1
                                             || a.InstanceStatus == InstanceStatus.Scheduled)
                                             select a;
                    break;

                case InstanceStatus.SuspendedAll:
                    omServiceInstancesEnum = from a in omServiceInstancesEnum
                                             where (a.InstanceStatus == InstanceStatus.SuspendedNotResumable
                                             || a.InstanceStatus == InstanceStatus.Suspended)
                                             select a;
                    break;

                default:
                     omServiceInstancesEnum = from a in omServiceInstancesEnum
                                             where a.InstanceStatus == instanceStatus
                                             select a;
                    break;

            }

            switch(ServiceClass)
            {
                case OM.ServiceClass.All:
                    //omServiceInstancesEnum = from a in omServiceInstancesEnum
                    //                         select a;
                    break;
                case OM.ServiceClass.MessagingAll:

                omServiceInstancesEnum = from a in omServiceInstancesEnum
                                         where a.Class ==  OM.ServiceClass.Messaging
                                         || a.Class == OM.ServiceClass.NonCreatableReceiver
                                         select a;
                break;

                default:
                omServiceInstancesEnum = from a in omServiceInstancesEnum
                                         where a.Class == ServiceClass
                                         select a;
                break;
            }

            if (ApplicationName != "All")
            {
                omServiceInstancesEnum = from a in omServiceInstancesEnum
                                         where a.Application == ApplicationName
                                         select a;
            }

            if (HostName != "All")
            {
                omServiceInstancesEnum = from a in omServiceInstancesEnum
                                         where a.HostName == HostName
                                         select a;
            }

            List<ServiceInstances> serviceInstances = new List<ServiceInstances>();

        
            //There are other examples to use Linq with Non Generic IEnumerables
            //But there is no advantage as the Operations.GetServiceInstances() returns all instances
            //Without any filter. So I can put Linq on List<ServiceInstances> for filtering query results

            int cnt = 0;
            foreach (OM.MessageBoxServiceInstance omServiceInstance in omServiceInstancesEnum)
            {
                if (cnt == maximumMatches)
                {
                    break;
                }
                    ServiceInstances serviceInstance = new ServiceInstances();
                    serviceInstance.ServiceName = omServiceInstance.ServiceType;
                    serviceInstance.Application = omServiceInstance.Application;
                    serviceInstance.ServiceClass = omServiceInstance.Class.ToString();
                    serviceInstance.Status = omServiceInstance.InstanceStatus.ToString();
                    serviceInstance.CreationTime = omServiceInstance.CreationTime.ToString();
                    serviceInstance.ErrorCode = omServiceInstance.ErrorCode;
                    serviceInstance.ErrorDescription = omServiceInstance.ErrorDescription;
                    serviceInstance.HostName = omServiceInstance.HostName;
                    serviceInstance.PendingJob = omServiceInstance.PendingOperation.ToString();

                    serviceInstances.Add(serviceInstance);

                    cnt++;
            }

           
            return serviceInstances;
        }


    }
}
